﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SmartSpace.UI.Silverlight.Dynamic.DynamicRef;
using System.Text;

namespace SmartSpace.UI.Silverlight.Dynamic.Controls
{
    /// <summary>
    /// 选择组织机构控件
    /// 可选择所有组织机构或者当前机构下所有机构
    /// </summary>
    public partial class SingleOrgControl : PMFDataControl
    {
        private bool m_isSubOnly = false;
        private string m_userName;

        public SingleOrgControl(bool subOnly, string userName)
        {
            m_isSubOnly = subOnly;
            m_userName = userName;

            InitializeComponent();
        }

        #region DependencyProperty

        public List<Org> SelectedOrgs
        {
            get { return (List<Org>)GetValue(SelectedOrgsProperty); }
            set { SetValue(SelectedOrgsProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedOrgsProperty =
            DependencyProperty.Register("SelectedOrgs", typeof(List<Org>), typeof(SingleOrgControl), new PropertyMetadata(default(List<Org>), OnSelectedOrgsPropertyChangedCallback));

        static void OnSelectedOrgsPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            SingleOrgControl ctl = d as SingleOrgControl;
            ctl.UpdateSelectedText();
        }

        public bool IsReadOnly
        {
            get { return (bool)GetValue(IsReadOnlyProperty); }
            set { SetValue(IsReadOnlyProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ItemsSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsReadOnlyProperty =
            DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(OrgListControl), new PropertyMetadata(default(bool), OnIsReadOnlyPropertyChangedCallback));

        static void OnIsReadOnlyPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            SingleOrgControl ctrl = d as SingleOrgControl;
            ctrl.SetReadOnly((bool)e.NewValue);
        }

        #endregion

        #region Overrides

        public override void UpdateSource()
        {
            this.GetBindingExpression(SingleOrgControl.SelectedOrgsProperty).UpdateSource();
        }

        private void _ctlMessage_MouseEnter(object sender, MouseEventArgs e)
        {
            _ctlMessage.Visibility = System.Windows.Visibility.Collapsed;
        }

        #endregion

        /// <summary>
        /// 选择组织机构按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnButtonBrowserClick(object sender, RoutedEventArgs e)
        {
            SelectOrgWindow window = new SelectOrgWindow(m_isSubOnly, m_userName, SelectedOrgs);
            window.Closed += new EventHandler(OnWindowClosed);
            window.Show();
        }

        void OnWindowClosed(object sender, EventArgs e)
        {
            SelectOrgWindow window = sender as SelectOrgWindow;
            if (window.DialogResult == true)
            {
                SelectedOrgs = window.SelectedOrgs;

                UpdateSelectedText();
            }
        }

        private void UpdateSelectedText()
        {
            StringBuilder textBuilder = new StringBuilder();
            for (int index = 0; index < SelectedOrgs.Count; index++)
            {
                Org org = SelectedOrgs[index];
                textBuilder.AppendFormat("{0}", org.Caption);
                if (index < SelectedOrgs.Count - 1)
                {
                    textBuilder.Append(",");
                }
            }

            m_textOrgName.Text = textBuilder.ToString();
        }

  

        private void SetReadOnly(bool isReadOnly)
        {
            if (isReadOnly)
            {
                m_btnBrowser.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                m_btnBrowser.Visibility = System.Windows.Visibility.Visible;
            }
        }
    }
}
